﻿using DFGK.WMS.Common.Extensions;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace DFGK_WMS.WebAPI.Controllers
{
    /// <summary>
    ///   基础数据
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class BasicDataController : ControllerBase
    {
        private readonly ISqlSugarClient _Client;

        public BasicDataController(ISqlSugarClient client)
        {
            _Client = client;
        }


        /// <summary>
        /// 根据TypeId获取单据类型
        /// </summary>
        /// <param name="type">1入库单  2出库单 3盘点单 4移库单</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetBillTypeById/{type}")]
        public IActionResult GetBillTypeById(int type = 1)
        {
            Type billType;
            if (type == 1)  //入库单
            {
                billType = typeof(EmInBillType);
            }
            else if (type == 2)  //出库单
            {
                billType = typeof(EmOutBillType);
            }
            else if (type == 3)  //盘库单
            {
                billType = typeof(EmStocktakeBillType);
            }
            else //移库单
            {
                billType = typeof(EmMoveBillType);
            }
            var result = billType.EnumToDictionary().Select(it => new SelectListItem
            {
                Value = it.Key,
                Text = it.Value

            }).ToList();

            return new JsonResult(BaseResult.Result(result, "200"));
        }

        /// <summary>
        /// 获取逻辑分区
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetLogicPartType")]
        public IActionResult GetLogicPartType()
        {
            try
            {
                var result =
                     _Client.Queryable<TDictItem>().AS("TDictItems").Where(t => t.TypeCode == "LogicPartType")
                    .Select(t => new SelectListItem
                    {
                        Id = t.Id,
                        Text = t.ItemCode,
                        Value = t.ItemName
                    })
                    .ToList();

                return new JsonResult(BaseResult.Result(result, "200"));
            }
            catch (Exception ex)
            {
                return new JsonResult(BaseResult.Result(ex.Message, "200"));
            }
        }
    }
}
